<template>
  <div>
    <div class="productManagement">
      <!-- 标题 -->
      <div class="productManagement-title">产品管理</div>


      <!-- 条件筛选 -->
      <div class="productManagement-condition">
        <div class="productManagement-condition-form">
          <normal-form-plus
            :formModel="formModel"
            :config="config"
            :plus="plus"/>
        </div>
        <div class="productManagement-condition-btn">
          <el-button>查询</el-button>
          <el-button>重置</el-button>
          <el-button @click="drawer = true">新增</el-button>
        </div>
      </div>


      <!-- 表格 -->
      <div class="productManagement-report">
        <normal-table
          :tableData="tableData"
          :paginationConfig="paginationConfig"
          :tableLabel="tableLabel"
          :operationConfig="operationConfig"
          :operationList="operationList"
          @event="handleEvent"
          @changePage="changePage"></normal-table>
      </div>

      <!-- 新增产品-抽屉 -->
      <el-drawer
        title="新增产品"
        :visible.sync="drawer"
        :direction="'rtl'">
        <normal-form
          :formModel="addFormModel"
          :config="addConfig"
          :plus="plus"/>
        <div class="drawer-btn">
          <el-button type="primary" @click="createProduct()">确认</el-button>
        </div>

      </el-drawer>


    </div>
  </div>
</template>

<script>
import NormalForm from '@/components/NormalForm'
import NormalTable from '@/components/NormalTable'
import NormalFormPlus from '@/components/NormalFormPlus'
import {createProduct, getProductByPage} from '@/api/product'

export default {
  name: 'productManagement',
  components: {
    NormalTable,
    NormalFormPlus,
    NormalForm
  },
  mounted() {
    this.getProductByPage()
  },
  data() {
    return {
      // 新增碳足迹报告-数据
      addFormModel: {
        encode: "",//	产品编码
        name: "",//		产品名称		true
        note: "",//		产品说明		false
        num: "",//		核算数量
      },
      addConfig: [
        {
          id: '1',
          type: 'input',
          model: 'name',
          name: '产品名称',
          edit: true,
          props: {
            placeholder: '请输入产品名称',
          }
        },
        {
          id: '2',
          type: 'input',
          model: 'spec',
          name: '产品规格',
          edit: true,
          props: {
            placeholder: '请输入产品规格',
          }
        },
        {
          id: '3',
          type: 'input',
          model: 'encode',
          name: '产品编码',
          edit: true,
          props: {
            placeholder: '请输入产品编码',
          }
        },
        {
          id: '4',
          type: 'text',
          model: 'note',
          name: '产品说明',
          edit: true,
          props: {
            placeholder: '请输入产品说明',
            rows: 5
          }
        },
      ],
      plus: {},
      drawer: false,
      formModel: {
        selection1: '',
        selection2: '',
        selection3: '',
      },
      config: [
        {
          id: 1,
          items: [
            {
              id: '1',
              edit: true,
              type: 'input',
              span: 6,
              model: 'modelLevel',
              name: '',
              props: {
                placeholder: "产品名称",

              }
            },
            // {
            //   id:'2',
            //   edit:true,
            //   type:'input',
            //   span:6,
            //   model:'modelLevel',
            //   name:'',
            //   props:{
            //     placeholder:"产品编码",

            //   }
            // },

          ]
        },
      ],
      plus: {},
      tableData: [],
      paginationConfig: {
        total: 100,
        page: 1,
        pageSize: 10,
        show: true
      },
      operationList: [
        {
          name: '编辑',
          order: 1,
          buttonClass: 'primary',
          plus: {}
        },
        {
          name: '删除',
          order: 2,
          buttonClass: 'danger',
          plus: {}
        }
      ],
      operationConfig: {select: false},
      tableLabel: [
        {
          prop: "order",
          label: "序号",
          width: 70
        },
        {
          prop: "name",
          label: "产品名称",
          width: 100
        },
        {
          prop: "spec",
          label: "产品规格",
          width: 300
        },
        {
          prop: "encode",
          label: "产品编码",
          // width:200
        },
        {
          prop: "state",
          label: "状态",
          width: 100,
          codeTable: {
            'true': '开启',
            'false': '关闭'
          }
        },
        {
          prop: "note",
          label: "产品说明",
          width: 300
        }
      ],

    }
  },
  methods: {
    // 添加产品
    createProduct() {
      this.drawer = false
      // this.$message({
      //   type: 'success',
      //   message: '添加成功'
      // });
      // this.tableData = this.$MOCK.product.productList2.data.list
      this.$API.product.createProduct({
        ...this.addFormModel,
        state: false
      }).then(response => {
        this.getProductByPage()
      })
    },
    // 获得产品分页
    getProductByPage() {
      if (this.$MOCK.mock) {
        this.tableData = this.$MOCK.product.productList.data.list
        this.paginationConfig.total = this.$MOCK.product.productList.data.total
        return
      }
      this.$API.product.getProductByPage({
        pageNo: this.paginationConfig.page,
        pageSize: this.paginationConfig.pageSize,
      }).then(response => {
        this.tableData = response.data.list
        this.paginationConfig.total = response.data.total
      })

    },
    // 处理表格点击事件
    handleEvent(order, row) {
      if (order === 1) {

      }
    },
    // 分页点击事件
    changePage() {
      this.getProductByPage()
    }
  }
}
</script>

<style lang="less" scoped>
.drawer-btn {
  margin-left: 150px;
}

.productManagement {

  height: 1880px;
  overflow: hidden;
  padding: 15px 10px;
  // 标题
  .productManagement-title {
    font-size: 16px;
    font-weight: bold;
  }

  .productManagement-condition {
    display: flex;

    .productManagement-condition-form {
      flex: 3;
    }

    .productManagement-condition-btn {
      flex: 1;
    }
  }

  // 核算报告表格
  .productManagement-report {
    .productManagement-history-title {
      font-size: 14px;
    }
  }
}
</style>
